<?php
	//***************************【layui 输出模板函数】***********************************
	
	// 表单-开关
	function lay_switch_tpl($title,$name,$val,$text="启用|禁用"){
		$data='<div class="layui-form-item"><label class="layui-form-label">'.$title.'</label><div class="layui-input-block">';
		$data.=lay_switch($name,$val,$text);
		$data.='</div></div>';
		return $data;
	}
	
	// switch 开关赋值代码块
	function lay_switch($name,$val,$text="启用|禁用"){
		$checked=$val==1?'checked':'';
		$data='<input type="checkbox" lay-skin="switch" value="'.$name.'" lay-text="'.$text.'" '.$checked.' ><input type="hidden" name="'.$name.'" value="'.$val.'"/>';
		return $data;
	}
	
	// 修改表格字段
	function lay_table_setval($table="dataTable"){
		$data='
			table.on(\'edit('.$table.')\', function(obj) {
				let value = obj.value;	//修改后的数据
				let field = obj.field;	//修改的字段名
				let data = obj.data;	//修改的当行数据
				let id = data.id;
				let loading = layer.load();
				let td = $(obj.tr.selector +" [data-field=" + field + "]");
			    $.ajax({
			        url:MODULE_PATH + \'setValue\',
			        data:{
						id:id,
						field:field,
						value:value,
					},
			        dataType: \'json\',
			        type: \'POST\',
			        success: function(res) {
			            layer.close(loading);
			            //判断有没有权限
			            if(res && res.code==999){
			                layer.msg(res.msg, {
			                    icon: 5,
			                    time: 2000, 
			                })
							td.addClass(\'pear-btn-danger\');
			                return false;
			            }else if (res.code==200) {
			                layer.msg(res.msg, {
			                    icon: 1,
			                    time: 1000, 
			                })
							td.addClass(\'pear-btn-primary\');
			            } else {
			                layer.msg(res.msg, {
			                    icon: 2,
			                    time: 1000
			                });
							td.addClass(\'pear-btn-danger\');
			            }
			        }
			    })
			});';
		return $data;
	}
	
    //搜索下拉菜单
    function lay_select_search_tpl($title,$name,$check,$list='',$value='',$text='',$verify='',$long=''){
    	$data='<div class="';
    	if($long==''){
    		$data.='layui-form-item';
    	}else{
    		$data.='layui-form-item2 layui-col-md'.$long;
    	}
    	$data.='"><label class="layui-form-label">'.$title.'</label><div class="layui-input-block">';
    	$data.='<div id="'.$name.'_search"></div><input name="'.$name.'" type="hidden" value="{$model.'.$name.'??\'\'}"/>';
    	$data.='</div></div>';
    	return $data;
    }
    
	// 表达-下拉菜单
	// 标题 / 表单ID / 已选值 / 数据集 / value的键名 / 文本的键名
	function lay_select_tpl($title,$name,$check,$list='',$value='',$text='',$verify='',$long=''){
		$data='<div class="';
		if($long==''){
			$data.='layui-form-item';
		}else{
			$data.='layui-form-item2 layui-col-md'.$long;
		}
		$data.='"><label class="layui-form-label">'.$title.'</label><div class="layui-input-block">';
		$data.=lay_select($name,$check,$list,$value,$text,$verify);
		$data.='</div></div>';
		return $data;
	}
	
	// 下拉菜单
	function lay_select($name,$check,$list='',$value='',$text='',$verify=''){
		if($list=='') $list=$name;
		if(!is_array($list)) $list=config_cache_file('options.'.$list);
		
		$data='<select name="'.$name.'" lay-verify=""';
		if ($verify!='') $data.='verify="'.$verify.'"';
		$data.='><option value=""></option>';
		$text=$text==''?$value:$text;
		foreach($list as $li){
			$v=isset($li[$value])?$li[$value]:$li;
			$t=isset($li[$text])?$li[$text]:$li;
			
			$data.='<option value="'.$v.'" ';
			if($v==$check){
				$data.="selected";
			}
			$data.='>'.$t.'</option>';
		}
		$data.='</select>';
		return $data;
	}
	
	// 表单-复选框
	function lay_checkbox_tpl($title,$name,$list,$check,$value='',$text='',$style=1){
		$data='<div class="layui-form-item"><label class="layui-form-label">'.$title.'</label><div class="layui-input-block">';
		$data.=lay_checkbox($name,$list,$check,$value,$text,$style);
		$data.='</div></div>';
		return $data;
	}
	
	// 复选框
	function lay_checkbox($name,$list,$check,$value='',$text='',$style){
		if(!is_array($list)) $list=config_cache_file('options.'.$list);
		if($check=='') $check=[];
		
		$data='';
		$text=$text==''?$value:$text;
		$style=$style==1?'lay-skin="primary"':'';
		foreach($list as $li){
			$v=isset($li[$value])?$li[$value]:$li;
			$t=isset($li[$text])?$li[$text]:$li;
			
			$data.='<input type="checkbox" name="'.$name.'[]" title="'.$t.'" '.$style.' value="'.$v.'"';

			if(in_array($v,$check)){
				$data.=" checked";
			}
			$data.='>';
		}
		return $data;
	}
	
	// 表单-单选框
	function lay_radio_tpl($title,$name,$list,$check,$value='',$text=''){
		$data='<div class="layui-form-item"><label class="layui-form-label">'.$title.'</label><div class="layui-input-block">';
		$data.=lay_radio($name,$list,$check,$value,$text);
		$data.='</div></div>';
		return $data;
	}
	
	// 单选框
	function lay_radio($name,$list,$check,$value='',$text=''){
		if(!is_array($list)) $list=config_cache_file('options.'.$list);
		$data='';
		$text=$text==''?$value:$text;
		foreach($list as $li){
			$v=isset($li[$value])?$li[$value]:$li;
			$t=isset($li[$text])?$li[$text]:$li;
			
			$data.='<input type="radio" name="'.$name.'" title="'.$t.'" value="'.$v.'" ';
			if($v==$check){
				$data.=" checked";
			}
			$data.='>';
		}
		return $data;
	}
	
	// 表单样式文本
	function lay_text_tpl($title,$name,$model,$verify='',$long=''){
		$value=isset($model[$name])?$model[$name]:'';
		$data='<div class="';
		if($long==''){
			$data.='layui-form-item';
		}else{
			$data.='layui-form-item2 layui-col-md'.$long;
		}
		$data.='"><label class="layui-form-label">
						'.$title.'
					</label>
					<div class="layui-input-block">
						<input type="text" class="layui-input layui-form-danger" name="'.$name.'" type="text" value="'.$value.'" ';
		if ($verify!='') $data.='verify="'.$verify.'"';
		$data.= '/>
					</div>
				</div>';
		return $data;
	}
	
	// 表单编辑器
	function lay_textarea_tpl($title,$name,$model,$verify=''){
		$value=isset($model[$name])?$model[$name]:'';
		$data='<div class="layui-form-item">
			<label class="layui-form-label">
				'.$title.'
			</label>
			<div class="layui-input-block">
				<textarea id="'.$name.'" name="'.$name.'" class="layui-textarea" type="text/plain" style="width:100%;margin-bottom:20px;" ';
		if ($verify!='') $data.='verify="'.$verify.'"';
				$data.='>'.$value.'</textarea>
			</div>
		</div>';
		return $data;
	}
	
	// 表单编辑器
	function lay_ueditor_tpl($title,$name,$model,$verify=''){
		$value=isset($model[$name])?$model[$name]:'';
		$data='<div class="layui-form-item">
			<label class="layui-form-label">
				'.$title.'
			</label>
			<div class="layui-input-block">
				<textarea id="'.$name.'" name="'.$name.'" type="text/plain" style="width:100%;margin-bottom:20px;" ';
		if ($verify!='') $data.='verify="'.$verify.'"';
				$data.='>'.$value.'</textarea>
			</div>
		</div>';
		return $data;
	}
	
	// 图片选择
	function lay_photo_tpl($title,$name,$model,$verify=''){
		$value=isset($model[$name])?$model[$name]:'';
		
		$data='
		<div class="layui-form-item layui-img-upload">
		    <label class="layui-form-label">
		        '.$title.'
		    </label>
		    <div class="layui-input-block">';
		
		$data.=opt_photo($name);
				
		$data.='<button class="pear-btn pear-btn-primary pear-btn-sm upload-image" type="button">
					<i class="icon pear-icon pear-icon-picture">
					</i>
					上传图片
				</button>
				<input name="'.$name.'" type="hidden" class="layui-input" value="'.$value.'" ';
		if($verify!='') $data.='verify="'.$verify.'"';
		$data.='/>
				<div class="upload-image">
					<span>
					</span>
					<img class="upload-image" src="'.$value.'"/>
				</div>
		    </div>
		</div>';
		return $data;
	}
	
	// 图片选择多项
	function lay_photos_tpl($title,$name,$model,$empty=0,$long='',$verify=''){
		if(!isset($model[$name])){
			$model[$name]=[];
			//return $name.'该参数不存在，无法生成图片选项';
		}
		
		if(!is_array($model[$name])){
			return $name.'该参数不是数组，请使用lay_photo_tpl函数';
		}
		
		$images=$model[$name];
		if(count($images)<$empty){
			$images=[];
			for($j=0;$j<$empty;$j++){
				$images[$j]='';
			}
		}
		
		$data='';
		foreach($images as $key=>$value){
			$i=$key+1;
			$data.='
			<div class="';
			if($long==''){
				$data.='layui-form-item layui-img-upload';
			}else{
				$data.='layui-form-item2 layui-img-upload layui-col-md'.$long;
			}
			$data.='">
			    <label class="layui-form-label">'.$title.$i.'</label>
			    <div class="layui-input-block">';
			
			$data.=opt_photo($name.$i);
			
			$data.='			
					<button class="pear-btn pear-btn-primary pear-btn-sm upload-image" type="button">
						<i class="icon pear-icon pear-icon-picture">
						</i>
						上传图片
					</button>
					<input name="'.$name.'['.$i.']" type="hidden" class="layui-input" value="'.$value.'" ';
			if ($verify!='') $data.='verify="'.$verify.'"';
			$data.='/>
					<div class="upload-image">
						<span>
						</span>
						<img class="upload-image" src="'.$value.'"/>
					</div>
				</div>
			</div>';
		}
		
		return $data;
	}
	
	//提交订单
	function lay_submit($name='dataTable',$success='',$error=''){
		$data="form.on('submit(save)', function (data) {
			
			let myform=$(this).parents('.layui-form');				//获取提交按钮的父表单
			let type=$(this).attr('type');							//判断验证的控件属性是否按钮
			myform.find('input').trigger('blur');					//验证表单内所有控件
			myform.find('textarea').trigger('blur');				//验证表单内所有控件
			myform.find('select').trigger('blur');					//验证表单内所有控件
			let numError=myform.find('.errorTips').length;
			if(numError){
				layer.msg('请先完善信息再提交表单',{icon:2,time:3000});
				return false;
			}
			
			let form_data = new FormData($('.layui-form')[0]);
			//load_show();
            $.ajax({
				data: form_data,
                dataType: 'json',
				processData: false,
                contentType: false,
                type: 'post',
                success: function (res) {
                    //判断有没有权限
                    if (res && res.code == 999) {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 2000,
                        })
                        return false;
                    } else if (res.code == 200) {
                        layer.msg(res.msg,{icon:1,time:1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload('".$name."');
							".$success."
                        });
                    } else {
                        layer.msg(res.msg,{icon:2,time:2500});
						".$error."
                    }
                },
				complete: function(){
					//load_hide();
				},
            })
            return false;
        });";
		return $data;
	}
?>